package cn.tedu.product.controller;


import cn.tedu.product.base.Utils;
import cn.tedu.product.base.response.JsonResult;
import cn.tedu.product.mapper.OrderDetailsMapper;
import cn.tedu.product.mapper.OrderMapper;
import cn.tedu.product.mapper.ProductMapper;
import cn.tedu.product.pojo.dto.*;
import cn.tedu.product.pojo.entity.Order;
import cn.tedu.product.pojo.entity.OrderDetails;
import cn.tedu.product.pojo.vo.OrderVO;
import cn.tedu.product.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("v1/orders/")
@Api(tags = "订单模块")
public class OrderController {
    @Autowired(required = false)
    private OrderMapper orderMapper;
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private OrderDetailsMapper orderDetailsMapper;
//    @Autowired
//    private RedisTemplate<String, Serializable> redisTemplate;
//
    @Autowired
    private ValueOperations<String, Serializable> ops;




    @PostMapping("selectOrdersAD")
    @ApiOperation(value = "管理员根据条件订单获取")
    public JsonResult getOrdersAD(@RequestBody  OrderDTO orderDTO){
        AdminSelectDTO orderSelectDTO=Utils.getorderSelectDTO(orderDTO);
        Boolean o1=Utils.ObjAllnotisNull(orderDTO);
        if (o1.equals(true)){
            //全都有  3 一起查
            List<OrderVO> orderVOS = orderMapper.selectOrdersByUidandStaandDate(orderSelectDTO);
            return JsonResult.ok(orderVOS);
        }

        List<String> o2= Utils.WhosnotNUll(orderDTO);

        if (o2.size()==1 || o2.size()==0){
            //有1个或 0个
            Object o3= Utils.ObjisNull(orderDTO);
            System.out.println(o3);
            if (o3.equals("userId")){
                return JsonResult.ok(orderMapper.selectOrdersByuId(orderDTO.getUserId()));
            }else if (o3.equals("status")){
                return JsonResult.ok(orderMapper.selectOrdersBystatus(orderDTO.getStatus()));
            }else if (o3.equals("createdAt")){
                return JsonResult.ok(orderMapper.selectOrdersByDate(orderSelectDTO));
            }else{
                return JsonResult.ok(orderMapper.selectOrders());
            }
        }else {
            //有两个
            if (!Utils.isConStr(o2,"userId")){
                return JsonResult.ok(orderMapper.selectOrdersByStaandDate(orderSelectDTO));
            }else if (!Utils.isConStr(o2,"createAt")) {
                return JsonResult.ok(orderMapper.selectOrdersByUidandSta(orderDTO));
            } else {
                return JsonResult.ok(orderMapper.selectOrdersByUidandDate(orderSelectDTO));
            }
        }

    }


    @PostMapping("selectOrdersUser")
    @ApiOperation(value = "用户根据条件订单获取")
    public JsonResult getOrdersUser(@RequestBody UserSelectDTO userSelectDTO, @ApiIgnore HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");

        AdminSelectDTO orderSelectDTO=Utils.getorderSelectDTO(userSelectDTO);

        Boolean o1=Utils.ObjAllnotisNull(userSelectDTO);
        if (o1.equals(true)){
            //全都有 2 一起查
            List<OrderVO> orderVOS = orderMapper.selectOrdersByUidandStaandDate(orderSelectDTO);
            return JsonResult.ok(orderVOS);
        }else {
            //有1个或 没有 0
            Object o3= Utils.ObjisNull(userSelectDTO);
            System.out.println(o3);
            if (o3.equals("status")){
                OrderDTO orderDTO=new OrderDTO();
                orderDTO.setUserId(userVO.getId());
                orderDTO.setStatus(userSelectDTO.getStatus());
                List<OrderVO> orderVOS = orderMapper.selectOrdersByUidandSta(orderDTO);
                return JsonResult.ok(orderVOS);
            }else if (o3.equals("createdAt")){
                orderSelectDTO.setUserId(userVO.getId());
                List<OrderVO> orderVOS = orderMapper.selectOrdersByUidandDate(orderSelectDTO);
                return JsonResult.ok(orderVOS);
            }else{
                return JsonResult.ok(orderMapper.selectOrdersByuId(userVO.getId()));
            }
        }
    }



    @PostMapping("creatOrder")
    @ApiOperation(value = "用户创建订单")
    public JsonResult creatOrder(@RequestBody @Validated BuyDTO buyDTO,@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        Double price=productMapper.selectPrice(buyDTO.getProductId());
        if (price==null)
            return JsonResult.notok("商品ID不存在");
        Double totalPrice=price*buyDTO.getQuantity();
        Order order=new Order();
        order.setUserId(userVO.getId());
        order.setTotalPrice(totalPrice);
        int i = orderMapper.insertOrder(order);
        if (i==1){
            Integer oid = orderMapper.selectLastOrderId(userVO.getId());
            OrderDetails orderDetails=new OrderDetails();
            BeanUtils.copyProperties(buyDTO,orderDetails);
            orderDetails.setOrderId(oid);
            int i1 = orderDetailsMapper.insertOD(orderDetails);
            if (i1==1)
                return JsonResult.ok();
            return JsonResult.notok();
        }
        return JsonResult.notok();
    }




    @PostMapping("updateStatus")
    @ApiOperation(value="修改订单状态")
    public JsonResult updatePayed(@RequestBody @Validated UpStatusDTO upStatusDTO){
        Serializable serializable = ops.get(upStatusDTO.getId()+":status");
        System.out.println(serializable);
        if (upStatusDTO.getStatus().equals(serializable)){
            System.out.println("走redis缓存");
            if (upStatusDTO.getStatus().equals("未支付"))
                return JsonResult.ok("无需重复设置为未支付");
            if (upStatusDTO.getStatus().equals("已支付"))
                return JsonResult.ok("无需重复支付");
            if (upStatusDTO.getStatus().equals("已发货"))
                return JsonResult.ok("无需重复发货");
            if (upStatusDTO.getStatus().equals("已签收"))
                return JsonResult.ok("无需重复签收");
        }
        int i = orderMapper.updateStatus(upStatusDTO);
        if (i==1){
            ops.set(upStatusDTO.getId()+":status",upStatusDTO.getStatus());
            Integer pid=orderMapper.selectPidByOid(upStatusDTO.getId());
            int i1 = productMapper.updateQuantity(pid);
            if (i1==1)
                return JsonResult.ok();
            return JsonResult.notok();
        }
        return JsonResult.notok("订单ID不存在");
    }

}
